package com.zmh.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import com.zmh.common.DateUtil;
import com.zmh.common.PropertiesUtil;
import com.zmh.common.TokenUtil;

/**
 * @author zhoumh
 * @version 创建时间：2017年6月23日 下午3:59:37
 * @Description
 */
@Configuration
public class WxMpConfig {
	@Value("#{wxProperties.wx_token}")
	private String token;

	@Value("#{wxProperties.wx_appid}")
	private String appid;

	@Value("#{wxProperties.wx_appsecret}")
	private String appsecret;

	@Value("#{wxProperties.wx_aeskey}")
	private String aesKey;

	@Value("#{wxProperties.wx_access_token}")
	private String wx_access_token;

	@Value("#{wxProperties.wx_expires_in}")
	private String wx_expires_in;

	public String getWx_access_token() {
		//获取配置文件中的时间戳与当前时间的差
		long time = DateUtil.getMinDiff(getWx_expires_in());
		//判断获取到的access_token时间是否超时
		if (time>=120) {
			//从微信获取最新wx_access_token
			wx_access_token = TokenUtil.getAccessToken();
			//更新配置文件中的wx_access_token与wx_expires_in
			PropertiesUtil.WriteProperties(this.getClass().getResource("/wx.properties").getPath(), "wx_expires_in", DateUtil.getDateTime());
			PropertiesUtil.WriteProperties(this.getClass().getResource("/wx.properties").getPath(), "wx_access_token", wx_access_token);
		}
		return this.wx_access_token;
	}

	public String getWx_expires_in() {
		//每次获取时间戳都从properties重新获取
		wx_expires_in=PropertiesUtil.ReadProperties(this.getClass().getResource("/wx.properties").getPath(), "wx_expires_in");
		return this.wx_expires_in;
	}

	public String getToken() {
		return this.token;
	}

	public String getAppid() {
		return this.appid;
	}

	public String getAppsecret() {
		return this.appsecret;
	}

	public String getAesKey() {
		return this.aesKey;
	}
}
